package com.example.monitoringsystem.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.monitoringsystem.bean.ExceptionPatient;
import com.example.monitoringsystem.bean.Health;
import com.example.monitoringsystem.mapper.ExceptionPatientMapper;
import com.example.monitoringsystem.mapper.HealthMapper;
import com.example.monitoringsystem.service.ExceptionHeanlthNotifyService;
import com.example.monitoringsystem.utils.DateUtils;
import com.example.monitoringsystem.utils.HealthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class ExceptionHeanlthNotifyServiceImpl implements ExceptionHeanlthNotifyService {
    @Autowired
    private ExceptionPatientMapper exceptionPatientMapper;

    @Autowired
    private HealthMapper healthMapper;

    // 获取最近一次识别异常信息的日期lastestDate
    private Date getLastestDate() {
        Date date = exceptionPatientMapper.getLastestDate();
        if (Objects.isNull(date)) date = DateUtils.getCurrentSqlDate();
        return date;
    }

    // 用来测试
// 获取最近一次识别异常信息的日期lastestDate
//private Date getLastestDate() {
//    Date date = healthMapper.selectById(2).getDate();
//    return date;
//}

    // 获取所有未识别的健康信息
    @Override
    public List<Health> getAllNotRecognizeHealth(){
        // 获取最近一次识别异常信息的日期lastestDate
        Date date = getLastestDate();
        // 从健康信息中选择日期大于lastestDate,的最新健康信息
        QueryWrapper<Health> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","temperature","pulse","blood_pressure","max(date) date","description");
        queryWrapper.between("date",date, DateUtils.getCurrentSqlDate());
        queryWrapper.groupBy("id");
        List<Health> healths = healthMapper.selectList(queryWrapper);
        for (Health health:healths){

        }
        return healths;
    }

    // 识别异常健康信息
    @Override
    public List<Health> getExceptionHealth(){
        List<Health> healths = getAllNotRecognizeHealth();
        return getExceptionHealth(healths);
    }

    // 识别异常健康信息
    @Override
    public List<Health> getExceptionHealth(List<Health> healths) {
        int i;
        int length = healths.size();
        for(i=length-1;i>=0;i--){
            if(HealthUtils.diagnosisHealth(healths.get(i)).length() == 0) healths.remove(i);
        }
        return healths;
    }

    // 发送通知
    @Override
    public void sendNotify() {
        List<Health> healths = getExceptionHealth();
        sendNotify(healths);
    }

    // 发送通知
    @Override
    public void sendNotify(List<Health> healths) {
        // 现在那到的是最新的，防止更改后再拿
        Date date = getLastestDate();
        // 更新通知表中的通知信息，做到增，删，改
        ExceptionPatient exceptionPatient;
        for (Health health:healths){
            // 当没有当前异常的患者时，增加
            exceptionPatient = exceptionPatientMapper.getExceptionById(health.getId());
            if(Objects.isNull(exceptionPatient)){
                exceptionPatient = new ExceptionPatient(health.getId(),health.getDate(),HealthUtils.diagnosisHealth(health),false);
                exceptionPatientMapper.insert(exceptionPatient);
                continue;
            }
            // 当有当前患者时，当患者的日期不是最新时，更新日期，并修改没有通知过
            else{
                if (exceptionPatient.getDate().before(health.getDate())){
                    exceptionPatient.setDate(health.getDate());
                    exceptionPatient.setIsGet(false);
                    exceptionPatientMapper.update(exceptionPatient, new UpdateWrapper<ExceptionPatient>().eq("patient_id",exceptionPatient.getPatientId())
                        );
                }
                // 当有患者时，日期最新，不进行更改,准确来说是不修改是否通知
            }
        }
        // 删除通知表中已经通知的，且日期不是今天的通知的日期范围
        System.out.println(date);
//        不知道为什么${}解析不了Date数据
//        exceptionPatientMapper.removeOldNotifiedException(date);
        exceptionPatientMapper.delete(new QueryWrapper<ExceptionPatient>().lt("date",date).eq("is_get",true));
    }

    // 医生获取自己的通知
    public List<ExceptionPatient> getNotify(Integer dcotorId) {
        // 获取通知后将获取项置为true
        List<ExceptionPatient> exceptionPatients = exceptionPatientMapper.getExceptionPatientsOfDoctor(dcotorId);
        for(ExceptionPatient exceptionPatient:exceptionPatients){
            exceptionPatient.setIsGet(true);
            exceptionPatientMapper.update(
                    exceptionPatient,
                    new UpdateWrapper<ExceptionPatient>().eq("patient_id",exceptionPatient.getPatientId())
            );
        }
        return exceptionPatients;
    }
}
